import React, { Component } from 'react';
import Detail from './Detail';
import { Link, Route } from 'react-router-dom/cjs/react-router-dom.min';

export default class Messages extends Component {
  state = {
    messageData: [
        {id: 1, title: '消息1'},
        {id: 2, title: '消息2'},
        {id: 3, title: '消息3'},
    ]
  }
  replaceShow = (id, title) => {
    // 携带params参数
    // return () => this.props.history.replace(`/home/messages/detail/${id}/${title}`)

    // 携带search参数
    // return () => this.props.history.replace(`/home/messages/detail/?id=${id}title=${title}`)

    // 携带state参数
    return () => this.props.history.replace(`/home/messages/detail`, {id, title})
  }

  pushShow = (id, title) => {
    // 携带params参数
    // return () => this.props.history.push(`/home/messages/detail/${id}/${title}`)

    // 携带search参数
    // return () => this.props.history.push(`/home/messages/detail/?id=${id}title=${title}`)

    // 携带state参数
    return () => this.props.history.replace(`/home/messages/detail`, {id, title})
  }

  render() {
    return (
      <div>
        <ul>
            {
                this.state.messageData.map(item => {
                    return (
                        <li key={item.id}>
                            {/* 向路由组件传递params参数 */}
                            {/* <Link to={`/home/messages/detail/${item.id}/${item.title}`}>{item.title}</Link> */}
                            
                            {/* 向路由组件传递search参数 */}
                            {/* <Link to={`/home/messages/detail/?id=${item.id}&title=${item.title}`}>{item.title}</Link> */}
                            
                            {/* 向路由组件传递state参数 */}
                            <Link to={{
                                pathname: '/home/messages/detail',
                                state: {
                                    id: item.id,
                                    title: item.title
                                }
                            }}>{item.title}</Link>

                            &nbsp;<button onClick={this.pushShow(item.id, item.title)}>push查看</button>
                            &nbsp;<button onClick={this.replaceShow(item.id, item.title)}>replace查看</button>
                        </li>
                    )
                })
            }
        </ul>
        {/* 声明接受params参数 */}
        {/* <Route path="/home/messages/detail/:id/:title" component={Detail}></Route> */}

        {/* search参数无需声明接受，正常注册路由即可 */}
        {/* <Route path="/home/messages/detail" component={Detail}></Route> */}

        {/* state参数无需声明接受，正常注册路由即可 */}
        <Route path="/home/messages/detail" component={Detail}></Route>
      </div>
    )
  }
}
